package com.aramis.mydytt.widget

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.appcompat.widget.AppCompatImageView
import androidx.constraintlayout.widget.ConstraintLayout
import com.aramis.mydytt.R
import java.lang.Exception

/**
 * Description: RatingBar
 * @author lizhidan
 * Created: 2021/5/8
 */
class RatingBar : ConstraintLayout {
    private val colorYellow = "#FFA500"
    private val colorGray = "#f5f5f5"
    private lateinit var starLayout: RelativeLayout
    private lateinit var scoreView: TextView
    private val starLength = 45

    constructor(context: Context) : super(context) {
        init()
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        init()
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        init()
    }

    private fun init() {
        LayoutInflater.from(context).inflate(R.layout.view_rating_bar, this, true)
        starLayout = findViewById(R.id.layout_rating_bar_star)
        scoreView = findViewById(R.id.text_rating_bar_score)
        addRatingStarView()
    }

    private fun addRatingStarView() {
        var lastViewId = 0
        (0..4).forEach { index ->
            val imageView = AppCompatImageView(context)
            val layoutParams = RelativeLayout.LayoutParams(starLength, starLength)
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP)
            if (lastViewId == 0) {
                layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT)
            } else {
                layoutParams.addRule(RelativeLayout.RIGHT_OF, lastViewId)
            }
            imageView.id = getStarId(index)
            imageView.layoutParams = layoutParams
            lastViewId = imageView.id
            starLayout.addView(imageView)
        }
    }

    fun setScore(score: String) {
        var decimal: Float
        var fullStarCount: Int
        var scoreStr: String
        try {
            val scoreF = score.toFloat()
            val scoreInt = scoreF.toInt()
            fullStarCount = scoreInt / 2
            decimal = (scoreF / 2f - fullStarCount)
            scoreStr = score
        } catch (e: Exception) {
            e.printStackTrace()
            decimal = 0f
            fullStarCount = 0
            scoreStr = "0.0"
        }
        scoreView.text = scoreStr
        (0..4).forEach {
            val imageView = starLayout.getChildAt(it) as AppCompatImageView
            if (it < fullStarCount) {
                imageView.setImageResource(R.drawable.ic_star)
                imageView.setColorFilter(Color.parseColor(colorYellow))
            } else if (it == fullStarCount && decimal >= 0.09f) {
                imageView.setImageResource(R.drawable.ic_star)
                imageView.setColorFilter(Color.parseColor(colorGray))

                val halfStarImageView = AppCompatImageView(context)
                halfStarImageView.setImageResource(R.drawable.ic_star_half)
                halfStarImageView.setColorFilter(Color.parseColor(colorYellow))
                val layoutParams = RelativeLayout.LayoutParams(starLength, starLength)
                layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP)
                layoutParams.addRule(RelativeLayout.ALIGN_LEFT, imageView.id)
                halfStarImageView.layoutParams = layoutParams
                starLayout.addView(halfStarImageView)
            } else {
                imageView.setImageResource(R.drawable.ic_star)
                imageView.setColorFilter(Color.parseColor(colorGray))
            }
        }
    }

    private fun getStarId(index: Int): Int = when (index) {
        0 -> R.id.id_rating_bar_half_bg0
        1 -> R.id.id_rating_bar_half_bg1
        2 -> R.id.id_rating_bar_half_bg2
        3 -> R.id.id_rating_bar_half_bg3
        else -> R.id.id_rating_bar_half_bg4
    }
}